package com.changanw.tienchin.web.controller.tienchin;

import com.changanw.tienchin.business.service.IBusinessService;
import com.changanw.tienchin.common.core.controller.BaseController;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.page.TableDataInfo;
import com.changanw.tienchin.contract.domain.Contract;
import com.changanw.tienchin.contract.domain.vo.ContractApproveInfo;
import com.changanw.tienchin.contract.domain.vo.ContractSummary;
import com.changanw.tienchin.contract.service.IContractService;
import com.changanw.tienchin.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author changanw
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/tienchin/contract")
public class ContractController extends BaseController {
  @Autowired
  IContractService contractService;
  @Autowired
  ISysUserService userService;
  @Autowired
  IBusinessService businessService;

  /**
   * 上传合同附件
   * @param request
   * @param file
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:create')")
  @PostMapping("/upload")
  public AjaxResult uploadContractFile(HttpServletRequest request, MultipartFile file){
    return contractService.uploadContractFile(request,file);
  }

  /**
   * 删除合同附件
   * @param year
   * @param month
   * @param day
   * @param name
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:create')")
  @DeleteMapping("/{year}/{month}/{day}/{name}")
  public AjaxResult deleteContractFile(@PathVariable String year, @PathVariable String month,@PathVariable String day,@PathVariable String name){
    return contractService.deleteContractFile(year,month,day,name);
  }

  /**
   * 添加合同
   * @param contract
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:create')")
  @PostMapping
  public AjaxResult addContract(@RequestBody Contract contract){
    return contractService.addContract(contract);
  }
  /**
   * 修改合同
   * @param contract
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:approve')")
  @PutMapping
  public AjaxResult updateContract(@RequestBody Contract contract){
    return contractService.updateContract(contract);
  }

  /**
   * 根据部门id获取用户
   * @param deptId
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:create')")
  @GetMapping("/users/{deptId}")
  public AjaxResult getUsersByDeptId(@PathVariable Long deptId){
    return userService.getUsersByDeptId(deptId);
  }

  /**
   * 根据可以手机 获取相关信息
   * @param phone
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:create')")
  @GetMapping("/customer/{phone}")
  public AjaxResult getCustomerNameByPhone(@PathVariable String phone){
    return contractService.geContractInfoPhone(phone);
  }

  /**
   * 获取当前用户未审批的任务
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:list')")
  @GetMapping("/unapprove")
  public TableDataInfo getUnApproveTask(){
    List<ContractSummary> unApproveTask = contractService.getUnApproveTask();
    return getDataTable(unApproveTask);
  }
  /**
   * 查询已经提交未审批的合同
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:list')")
  @GetMapping("/committed_task")
  public TableDataInfo getCommittedTask(){
    List<ContractSummary> unApproveTask = contractService.getCommittedTask();
    return getDataTable(unApproveTask);
  }

  /**
   * 查看合同信息
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:view')")
  @GetMapping("/views/{contractId}")
  public AjaxResult getContractById(@PathVariable Integer contractId){
    return contractService.getContractById(contractId);
  }


  /**
   * 预览 pdf文件
   * @param year
   * @param month
   * @param day
   * @param name
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:view')")
  @GetMapping("/views/{year}/{month}/{day}/{name}")
  public AjaxResult showContractPDF(@PathVariable String year, @PathVariable String month, @PathVariable String day, @PathVariable String name){
    return contractService.showContractPDF(year,month,day,name);
  }

  /**
   * 合同审批或者驳回
   * @param contractApproveInfo
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:approve')")
  @PostMapping("/approve")
  public AjaxResult approveOrReject(@RequestBody ContractApproveInfo contractApproveInfo){
    return contractService.approveOrReject(contractApproveInfo);
  }

  /**
   * 展示当前用户提交的以及审批通过的合同
   * @param contractApproveInfo
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:contract:approve')")
  @GetMapping("/approved")
  public TableDataInfo approvedTask(){
    startPage();
    List<ContractSummary> approvedTask = contractService.approvedTask();
    return getDataTable(approvedTask);
  }

}
